package tablet.module_user.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.json_deal.lib_mgson.MGson;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.load_state_view.MultiStateView;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.fastkit.views.spring_refresh.container.DefaultFooter;
import com.lib.fastkit.views.spring_refresh.container.DefaultHeader;
import com.lib.fastkit.views.spring_refresh.widget.SpringView;
import com.lib.http.call_back.HttpDialogCallBack;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.activity.BaseAppActivity;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.utls.glide.GlideConfig;
import com.lib.view.navigationbar.NomalNavigationBar;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import tablet.module_user.R;
import tablet.module_user.R2;
import tablet.module_user.bean.StudentCollectedVideoBean;
import tablet.module_user.bean.StudentWorkRecordBean;
import tablet.module_user.fragment.StudentInfoRecordFragment;

@Route(path = ARouterPathUtils.User_StudentListDataShowActivity)
public class StudentListDataShowActivity extends BaseAppActivity {
    @BindView(R2.id.rv_video)
    RecyclerView rvVideo;
    @BindView(R2.id.springView)
    SpringView springView;
    @BindView(R2.id.state_view)
    MultiStateView stateView;

    @Autowired(name = "showlistdata")
    int showlistdata = -1;

    //用户记录作业历史详情的头部地址
    public String recordWorkHeadUrl = "";

    int page = 0;
    HomeAdapter homeAdapter;
    List<StudentCollectedVideoBean.ObjBean.RowsBean> list = new ArrayList<>();

    StudentWorkRecordAdapter recordAdapter;
    List<StudentWorkRecordBean.ObjBean.JobRecordBean.RowsBean> recordList = new ArrayList<>();

    @Override
    protected void onCreateView() {

        stateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        stateView.setMultiStateViewLisener(new MultiStateView.MultiStateViewLisener() {
            @Override
            public void onTryAgain() {
                judgeShowListDataType();
            }
        });

        initView();

        showlistdata = getIntent().getIntExtra("showlistdata", -1);
        LogUtil.e("获取到要显示的列表数据为：" + showlistdata);

        judgeShowListDataType();
    }


    private void judgeShowListDataType() {

        if (showlistdata > -1) {

            //0代表显示作业记录
            if (0 == showlistdata) {

                //获取学生作业记录数据
                initUserWorkRecordData();

                recordAdapter = new StudentWorkRecordAdapter(this, recordList);
                MyLinearLayoutManager manager = new MyLinearLayoutManager(this);
                rvVideo.setLayoutManager(manager);
                rvVideo.setAdapter(recordAdapter);

                NomalNavigationBar navigationBar = new
                        NomalNavigationBar.Builder(this)
                        .setTitle("作业记录")
                        .builder();

            } else {

                if (1 == showlistdata) { //1代表收藏的录播课记录

                    initStudentCollectResData(0);

                } else if (2 == showlistdata) { //2代表收藏的教辅资料记录

                    initStudentCollectResData(1);
                } else if (3 == showlistdata) { //3代表收藏的名师资源记录

                    initStudentCollectResData(2);
                }

                homeAdapter = new HomeAdapter(this, list);
                GridLayoutManager manager = new GridLayoutManager(this, 4);
                rvVideo.setLayoutManager(manager);
                rvVideo.setAdapter(homeAdapter);

                NomalNavigationBar navigationBar = new
                        NomalNavigationBar.Builder(this)
                        .setTitle("收藏记录")
                        .builder();

            }

        }

    }

    private void initUserWorkRecordData() {

        HttpUtils.with(this)
                .addParam("requestType", "JOB_RECORD")
                .addParam("page", page)
                .addParam("limit", 10)
                .addParam("token", SharedPreferenceManager.getInstance(this).getUserCache().getUserToken())
                .execute(new HttpNormalCallBack<StudentWorkRecordBean>() {
                    @Override
                    public void onSuccess(StudentWorkRecordBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {

                            if (StringUtil.isNotEmpty(result.getObj().getJob_record_url(), true)) {
                                recordWorkHeadUrl = result.getObj().getJob_record_url();
                            }

                            if (result.getObj().getJob_record().getRows().size() > 0) {

                                if (page == 0 && recordList.size() > 0) {
                                    recordList.clear();
                                }

                                recordList.addAll(result.getObj().getJob_record().getRows());
                                recordAdapter.notifyDataSetChanged();

                            }

                            stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {

                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        }
                    }

                    @Override
                    public void onError(String e) {

                        if (stateView != null) {
                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        }
                    }
                });


    }

    private void initStudentCollectResData(int type) {

        HttpUtils.with(this)
                .addParam("requestType", "STUDENT_FAVORITE_LIST")
                .addParam("token", SharedPreferenceManager.getInstance(this).getUserCache().getUserToken())
                .addParam("page", page)
                .addParam("limit", 10)
                .addParam("lbk_or_jfzl", type)
                .execute(new HttpDialogCallBack<StudentCollectedVideoBean>() {
                    @Override
                    public void onSuccess(StudentCollectedVideoBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {
                            if (result.getObj() != null) {
                                if (page == 0 && list.size() > 0) {
                                    list.clear();
                                }
                                list.addAll(result.getObj().getRows());
                                homeAdapter.notifyDataSetChanged();
                            }

                            stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {

                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        }
                    }

                    @Override
                    public void onError(String e) {
                        stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                    }
                });


    }

    private void initView() {

        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));

        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {

                page = 0;
                judgeShowListDataType();
                springView.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {

                ++page;
                judgeShowListDataType();
                springView.onFinishFreshAndLoad();
            }
        });


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_userecord;
    }


    private class HomeAdapter extends BaseAdapter<StudentCollectedVideoBean.ObjBean.RowsBean> {

        public HomeAdapter(Context context, List<StudentCollectedVideoBean.ObjBean.RowsBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_userecord;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<StudentCollectedVideoBean.ObjBean.RowsBean> mData) {

            final StudentCollectedVideoBean.ObjBean.RowsBean rowsBean = mData.get(position);

            ImageView ivBg = holder.getView(R.id.iv_record);
            if (StringUtil.isNotEmpty(rowsBean.getVideo_cover(), true)) {

                Glide.with(StudentListDataShowActivity.this)
                        .load(rowsBean.getVideo_cover())
                        .apply(GlideConfig.getRoundOptions(10))
                        .into(ivBg);
            }
            if (StringUtil.isNotEmpty(rowsBean.getVideo_title(), true)) {
                holder.setText(R.id.tv_record, rowsBean.getVideo_title());
            }

            LinearLayout linItem = holder.getView(R.id.lin_item);
            linItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String recordJson = MGson.newGson().toJson(rowsBean);
                    LogUtil.e("录播课视频的数据为：" + recordJson);

                    ARouter.getInstance().build(ARouterPathUtils.Tablet_Video_RecordDetailActivity)
                            .withInt("recordId", rowsBean.getId())
                            .withString("recordJson", recordJson)
                            .withString("showType", "UserCollect").navigation();

                }
            });
        }

    }

    class StudentWorkRecordAdapter extends BaseAdapter<StudentWorkRecordBean.ObjBean.JobRecordBean.RowsBean> {


        public StudentWorkRecordAdapter(Context context, List<StudentWorkRecordBean.ObjBean.JobRecordBean.RowsBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_workrecord;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, int position, List<StudentWorkRecordBean.ObjBean.JobRecordBean.RowsBean> mData) {

            final StudentWorkRecordBean.ObjBean.JobRecordBean.RowsBean rowsBean = mData.get(position);

            if (StringUtil.isNotEmpty(rowsBean.getTeacher_name(), true)) {
                holder.setText(R.id.tv_name, rowsBean.getTeacher_name());
            }

            if (StringUtil.isNotEmpty(rowsBean.getCreation_date(), true)) {
                holder.setText(R.id.tv_date, rowsBean.getCreation_date());
            }

            TextView tvDetail = holder.getView(R.id.tv_detail);
            LinearLayout linItem = holder.getView(R.id.lin_item);
//            tvDetail.setVisibility(View.GONE);
            linItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (StringUtil.isNotEmpty(recordWorkHeadUrl, true)) {
                        String recordUrl = recordWorkHeadUrl +
                                SharedPreferenceManager.getInstance(StudentListDataShowActivity.this).getUserCache().getUserPhone() +
                                "/" + rowsBean.getCreation_date();

                        ARouter.getInstance().build(ARouterPathUtils.Tablet_App_NormalDetailWebActivity)
                                .withString("urlPath", recordUrl).navigation();
                    }
                }
            });
        }
    }
}
